//
//  MengwDetailsTableViewController.m
//  PetTown
//
//  Created by lanou3g on 15/6/4.
//  Copyright (c) 2015年 lanou3g. All rights reserved.
//

#import "MengwDetailsTableViewController.h"
@interface MengwDetailsTableViewController ()

@end

@implementation MengwDetailsTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.allDataArray = [[NSMutableArray alloc] init];
    _a = 1;
    //下拉刷新
    [self.tableView addHeaderWithTarget:self action:@selector(headerRereshing)];
    //上啦刷新
    [self.tableView addFooterWithTarget:self action:@selector(footerRereshing)];
    [self headerView];
    
    [self JSOn];
    [self returns];

    self.navigationItem.rightBarButtonItem.title = @"故事";
    
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}
//设置返回按钮
- (void)returns{
    UIButton * returnBu = [UIButton buttonWithType:(UIButtonTypeSystem)];
    returnBu.frame = CGRectMake(0, 0, 32, 32);
    [returnBu setBackgroundImage:[UIImage imageNamed:@"ReturnPage.png"] forState:UIControlStateNormal];
    [returnBu addTarget:self action:@selector(returnBueeon:) forControlEvents:UIControlEventTouchDown];
    UIBarButtonItem * leftBu = [[UIBarButtonItem alloc] initWithCustomView:returnBu];
    self.navigationItem.leftBarButtonItem = leftBu;
}
//点击返回按钮
- (void)returnBueeon:(UIBarButtonItem *)leftButton{
    [self.navigationController popViewControllerAnimated:YES];
    
}
//设置表透视图
- (void)headerView{
    NSString * uuu = [NSString stringWithFormat:PetRankingDetailsQI,self.ppid];
    NSURL * url = [NSURL URLWithString:uuu];
    NSURLRequest * request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:60.0];
    
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        if (data == nil) {
            return;
        }

        NSMutableDictionary * dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        NSMutableDictionary * dic1 = dic[@"data"];
        NSDictionary * dic2 = dic1[@"photo"];

    CGFloat pic_h = [[UIScreen mainScreen] bounds].size.width * [dic2[@"picheight"] floatValue] / [dic2[@"picwidth"] floatValue];
    //设置表头视图(详情里面最上面的部分)
    UIView * cell_tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, pic_h+150)];
    self.tableView.tableHeaderView = cell_tableHeaderView;
    //设置表尾视图(最后一条了)
    UILabel * cell_tableFootLable = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.tableView.frame.size.width, 40)];
    cell_tableFootLable.text = @"上拉加载";
    cell_tableFootLable.textAlignment = NSTextAlignmentCenter;
    cell_tableFootLable.layer.borderWidth = 0.5;
    self.tableView.tableFooterView = cell_tableFootLable;
    //设置表头视图内容
    //图片
    UIImage * imm = [UIImage imageNamed:@"place.jpg"];
    UIImageView * header_imageView = [[UIImageView alloc] init];
    [header_imageView sd_setImageWithURL:[NSURL URLWithString:dic2[@"photo"]] placeholderImage:imm];
    header_imageView.frame = CGRectMake(0, 0, cell_tableHeaderView.frame.size.width, pic_h);
    [cell_tableHeaderView addSubview:header_imageView];
    //用户头像
    UIImageView * user_avatarImageView = [[UIImageView alloc] init];
    [user_avatarImageView sd_setImageWithURL:[NSURL URLWithString:dic2[@"avatar"]] placeholderImage:imm];
    user_avatarImageView.frame = CGRectMake(20, pic_h-20, 50, 50);
    user_avatarImageView.backgroundColor = [UIColor purpleColor];
    [cell_tableHeaderView addSubview:user_avatarImageView];
    //昵称
    UILabel * user_disnameLable = [[UILabel alloc] initWithFrame:CGRectMake(90, pic_h, 150, 50)];
    user_disnameLable.text = dic2[@"disname"];
    [cell_tableHeaderView addSubview:user_disnameLable];
    //发布时间
    UILabel * user_timeLable = [[UILabel alloc] initWithFrame:CGRectMake(cell_tableHeaderView.frame.size.width-100, pic_h, 100, 50)];
    user_timeLable.text = dic2[@"time"];
    [cell_tableHeaderView addSubview:user_timeLable];
    //描述
    UILabel * user_contentLable = [[UILabel alloc] initWithFrame:CGRectMake(10, pic_h+50, cell_tableHeaderView.frame.size.width-20, 100)];
        user_contentLable.textAlignment = NSTextAlignmentCenter;
        user_contentLable.layer.cornerRadius = 5.0;
        user_contentLable.font = [UIFont systemFontOfSize:14];
    user_contentLable.text = dic2[@"photo_des"];
    if ([user_contentLable.text isEqual: @""]){
        user_contentLable.text = @"小主人好懒,什么都不写!";
    }
    user_contentLable.numberOfLines = 0;
    user_contentLable.layer.borderWidth = 0.5;
    [cell_tableHeaderView addSubview:user_contentLable];
        
        [self.tableView reloadData];
    }];
}
//下拉刷新
- (void)headerRereshing{
    
    [self.tableView headerEndRefreshing];
}
//上拉刷新
-(void)footerRereshing{
    _a++;
    [self JSOn];
    [self.tableView footerEndRefreshing];
}
#pragma mark----数据解析
- (void)JSOn{
    NSString * uu = [NSString stringWithFormat:PetRankingDetailsCommentQI,_ppid,[NSString stringWithFormat:@"%d",_a]];
    
    
    NSURL * url = [NSURL URLWithString:uu];
    
    NSURLRequest * request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:60.0];
    
    __weak MengwDetailsTableViewController * MDTVC = self;
    
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        if (data == nil) {
            return;
        }
        
        NSMutableDictionary * dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        NSMutableDictionary * dic1 = dic[@"data"];
        NSArray * array = dic1[@"list"];
        for (NSDictionary * modelDic in array) {
            Reviewer * reviewer = [[Reviewer alloc] init];
            [reviewer setValuesForKeysWithDictionary:modelDic];
            [MDTVC.allDataArray addObject:reviewer];
        }
        [self.tableView reloadData];
    }];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
    //清除缓存
    SDImageCache * sdI = [SDImageCache sharedImageCache];
    [sdI clearDisk];
    [sdI clearMemory];
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
#warning Potentially incomplete method implementation.
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
#warning Incomplete method implementation.
    // Return the number of rows in the section.
    return _allDataArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ChoicenessDetailsCell *cell = [tableView dequeueReusableCellWithIdentifier:@"homePageNewCell"];
    if (!cell) {
        cell = [[ChoicenessDetailsCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"homePageNewCell"];
    }
    Reviewer * rr = _allDataArray[indexPath.row];
    cell.reviewer = rr;
    
    // Configure the cell...
    
    return cell;
}

//设置cell高度
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 100;
}
/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
